home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Database How-To / Visual Basic 4 Database - How-to (The Waite Group)(1995).iso / register.fr_ / register.fr
Text File  |  1995-07-19  |  9KB  |  319 lines

  1. VERSION 4.00
  2. Begin VB.Form frmODBC 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "ODBC Data Sources"
  5.    ClientHeight    =   2970
  6.    ClientLeft      =   1320
  7.    ClientTop       =   2490
  8.    ClientWidth     =   8490
  9.    Height          =   3465
  10.    Left            =   1215
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   2970
  13.    ScaleWidth      =   8490
  14.    Top             =   2100
  15.    Width           =   8700
  16.    Begin VB.CommandButton cmdCreateDSN 
  17.       Caption         =   "&New Data Source"
  18.       Height          =   495
  19.       Left            =   5160
  20.       TabIndex        =   8
  21.       Top             =   2280
  22.       Width           =   1455
  23.    End
  24.    Begin VB.Frame Frame1 
  25.       BackColor       =   &H00C0C0C0&
  26.       Caption         =   "New Data Source"
  27.       Height          =   1815
  28.       Left            =   240
  29.       TabIndex        =   10
  30.       Top             =   240
  31.       Width           =   4695
  32.       Begin VB.TextBox txtDSNdesc 
  33.          Height          =   285
  34.          Left            =   1800
  35.          TabIndex        =   3
  36.          Top             =   840
  37.          Width           =   2655
  38.       End
  39.       Begin VB.TextBox txtDSNname 
  40.          Height          =   285
  41.          Left            =   1800
  42.          TabIndex        =   1
  43.          Top             =   360
  44.          Width           =   2655
  45.       End
  46.       Begin VB.ComboBox lstODBCdrivers 
  47.          Height          =   300
  48.          Left            =   1800
  49.          Sorted          =   -1  'True
  50.          Style           =   2  'Dropdown List
  51.          TabIndex        =   5
  52.          Top             =   1320
  53.          Width           =   2655
  54.       End
  55.       Begin VB.Label Label3 
  56.          Alignment       =   1  'Right Justify
  57.          BackColor       =   &H00C0C0C0&
  58.          Caption         =   "Select ODBC Driver:"
  59.          Height          =   255
  60.          Left            =   120
  61.          TabIndex        =   4
  62.          Top             =   1320
  63.          Width           =   1575
  64.       End
  65.       Begin VB.Label Label2 
  66.          Alignment       =   1  'Right Justify
  67.          BackColor       =   &H00C0C0C0&
  68.          Caption         =   "Description:"
  69.          Height          =   255
  70.          Left            =   120
  71.          TabIndex        =   2
  72.          Top             =   840
  73.          Width           =   1575
  74.       End
  75.       Begin VB.Label Label4 
  76.          Alignment       =   1  'Right Justify
  77.          BackColor       =   &H00C0C0C0&
  78.          Caption         =   "Name:"
  79.          Height          =   255
  80.          Left            =   120
  81.          TabIndex        =   0
  82.          Top             =   360
  83.          Width           =   1575
  84.       End
  85.    End
  86.    Begin VB.CommandButton cmdQuit 
  87.       Caption         =   "&Quit"
  88.       Height          =   495
  89.       Left            =   7080
  90.       TabIndex        =   9
  91.       Top             =   2280
  92.       Width           =   1215
  93.    End
  94.    Begin VB.ListBox lstODBCdbs 
  95.       BackColor       =   &H00C0C0C0&
  96.       Height          =   1590
  97.       Left            =   5160
  98.       Sorted          =   -1  'True
  99.       TabIndex        =   7
  100.       TabStop         =   0   'False
  101.       Top             =   480
  102.       Width           =   3135
  103.    End
  104.    Begin VB.Label Label1 
  105.       BackColor       =   &H00C0C0C0&
  106.       Caption         =   "Installed ODBC Data Sources:"
  107.       Height          =   255
  108.       Left            =   5160
  109.       TabIndex        =   6
  110.       Top             =   240
  111.       Width           =   2535
  112.    End
  113. End
  114. Attribute VB_Name = "frmODBC"
  115. Attribute VB_Creatable = False
  116. Attribute VB_Exposed = False
  117. Option Explicit
  118.  
  119. 'Dynamic arrays to hold data
  120. Dim dbName() As String
  121. Dim dbDesc() As String
  122. Dim DriverDesc() As String
  123. Dim DriverAttr() As String
  124.  
  125. Private Sub cmdCreateDSN_Click()
  126.     CreateNewDSN
  127. End Sub
  128.  
  129. Private Sub cmdQuit_Click()
  130.     End
  131. End Sub
  132.  
  133. Private Sub Form_Load()
  134.     'Log on to an ODBC data source
  135.     'First, allocate ODBC memory and get handles
  136.     Dim result As Integer
  137.         
  138.     'Center the form
  139.     Centerform
  140.     
  141.     'Allocate the ODBC environment handle
  142.     result = ODBCAllocateEnv(ghEnv)
  143.     If result <> SQL_SUCCESS Then
  144.         Exit Sub
  145.     End If
  146.     
  147.     'Load the current list of data sources to list box
  148.     GetODBCdbs
  149.     
  150.     'Get the list of installed drivers
  151.     GetODBCdvrs
  152.     lstODBCDrivers.ListIndex = 0
  153.     
  154.     frmODBC.Show
  155.     txtDSNname.SetFocus
  156. End Sub
  157.  
  158. Private Sub GetODBCdbs()
  159.     Dim cbDSNMax As Integer
  160.     Dim szDSN As String * 33
  161.     #If Win32 Then
  162.         Dim pcbDSN As Long
  163.         Dim pcbDescription As Long
  164.     #Else
  165.         Dim pcbDSN As Integer
  166.         Dim pcbDescription As Integer
  167.     #End If
  168.     Dim szDescription As String * 512
  169.     Dim cbDescriptionMax As Integer
  170.     Dim result As Integer
  171.     Dim i As Integer
  172.     Dim nameLen As Integer
  173.     Dim ErrResult
  174.     Dim savecursor
  175.     
  176.     'Clear out the contents of the list box
  177.     lstODBCdbs.Clear
  178.     
  179.     cbDSNMax = SQL_MAX_DSN_LENGTH + 1
  180.     cbDescriptionMax = 512
  181.     result = SQL_SUCCESS
  182.     i = 0
  183.  
  184.     savecursor = Screen.MousePointer
  185.     Screen.MousePointer = HOURGLASS
  186.     Do While result <> SQL_NO_DATA_FOUND
  187.         'Get the next data source (using SQL_FETCH_NEXT
  188.         'on the first call to SQLDataSourcesgets gets
  189.         'the first data source).
  190.         result = SQLDataSources(ghEnv, SQL_FETCH_NEXT, szDSN, cbDSNMax, pcbDSN, szDescription, cbDescriptionMax, pcbDescription)
  191.         If result = SQL_ERROR Then
  192.             ErrResult = ODBCError("Env", ghEnv, 0, 0, result, "Error getting list of data sources.")
  193.             Screen.MousePointer = savecursor
  194.             Exit Sub
  195.         End If
  196.         
  197.         'Add the data source data to the global arrays
  198.         ReDim Preserve dbName(i)
  199.         dbName(i) = Left(szDSN, pcbDSN)
  200.         ReDim Preserve dbDesc(i)
  201.         dbDesc(i) = Left(szDescription, pcbDescription)
  202.         
  203.         lstODBCdbs.AddItem dbName(i) & "  (" & dbDesc(i) & ")"
  204.         
  205.         i = i + 1
  206.     Loop
  207.     Screen.MousePointer = savecursor
  208.  
  209. End Sub
  210.  
  211. Private Sub GetODBCdvrs()
  212.     Dim szDriverDesc As String * 512
  213.     Dim cbDriverDescMax As Integer
  214.     #If Win32 Then
  215.         Dim pcbDriverDesc As Long
  216.         Dim pcbDrvrAttr As Long
  217.     #Else
  218.         Dim pcbDriverDesc As Integer
  219.         Dim pcbDrvrAttr As Integer
  220.     #End If
  221.     Dim szDriverAttributes As String * 2048
  222.     Dim cbDrvrAttrMax As Integer
  223.     Dim i As Integer
  224.     Dim result As Integer
  225.     Dim ErrResult As Integer
  226.     Dim savecursor
  227.  
  228.     cbDriverDescMax = 512
  229.     cbDrvrAttrMax = 2048
  230.     result = SQL_SUCCESS
  231.     i = 0
  232.  
  233.     savecursor = Screen.MousePointer
  234.     Screen.MousePointer = HOURGLASS
  235.     Do While result <> SQL_NO_DATA_FOUND
  236.         'On the first call to SQLDrivers, using
  237.         'SQL_FETCH_NEXT gets the first driver.
  238.         result = SQLDrivers(ghEnv, SQL_FETCH_NEXT, szDriverDesc, cbDriverDescMax, pcbDriverDesc, szDriverAttributes, cbDrvrAttrMax, pcbDrvrAttr)
  239.         If result = SQL_ERROR Then
  240.             ErrResult = ODBCError("Env", ghEnv, 0, 0, result, "Error getting list of registered drivers.")
  241.             'Screen.MousePointer = save
  242.             Exit Sub
  243.         End If
  244.  
  245.         'Add the driver information to the data arrays,
  246.         'and add to the lstODBCDrivers list box.
  247.         ReDim Preserve DriverDesc(i)
  248.         DriverDesc(i) = Left(szDriverDesc, pcbDriverDesc)
  249.         ReDim Preserve DriverAttr(i)
  250.         DriverAttr(i) = Left(szDriverAttributes, pcbDrvrAttr)
  251.         
  252.         lstODBCDrivers.AddItem DriverDesc(i) & "  (" & DriverAttr(i) & ")"
  253.         
  254.         i = i + 1
  255.     Loop
  256.     
  257.     Screen.MousePointer = savecursor
  258.  
  259. End Sub
  260.  
  261. Private Sub Form_Unload(Cancel As Integer)
  262.     Dim result As Integer
  263.     
  264.     'Clean up the ODBC connections that we allocated
  265.     'and opened.
  266.     result = ODBCDisconnectDS(ghEnv, ghDbc, ghStmt)
  267.     result = ODBCFreeEnv(ghEnv)
  268. End Sub
  269.  
  270. Sub CreateNewDSN()
  271.     'Add a new data source name to the ODBC system
  272.     Dim DSNname As String
  273.     Dim DSNattrib As String
  274.     Dim DSNdriver As String
  275.     Dim result As Integer
  276.     Dim savecursor
  277.     
  278.     If txtDSNname = "" Then
  279.         MsgBox "You must enter a name for the new data source."
  280.         txtDSNname.SetFocus
  281.     Else
  282.         savecursor = Screen.MousePointer
  283.         Screen.MousePointer = HOURGLASS
  284.  
  285.         'Format the arguments to RegisterDatabase
  286.         DSNname = txtDSNname.text
  287.         DSNattrib = "Description=" & txtDSNdesc.text
  288.         DSNdriver = lstODBCDrivers.List(lstODBCDrivers.ListIndex)
  289.     
  290.         On Error GoTo CantRegister
  291.         'Trap any errors so we can respond to them
  292.         DBEngine.RegisterDatabase DSNname, DSNdriver, False, DSNattrib
  293.         On Error GoTo 0
  294.         
  295.         'Now, rebuild the list of data source names
  296.         GetODBCdbs
  297.         
  298.         Screen.MousePointer = savecursor
  299.     End If
  300.     
  301.     Exit Sub
  302.     
  303. CantRegister:
  304.     If Err.Number = 3146 Then
  305.         'ODBC couldn't find the setup driver specified
  306.         'for this database in ODBCINST.INI.
  307.         MsgBox "Cannot find driver installation DLL.", MB_ICONSTOP
  308.         Resume Next
  309.     Else
  310.         Error Err.Number
  311.     End If
  312.     
  313. End Sub
  314.  
  315. Sub Centerform()
  316.     frmODBC.Move (Screen.Width - frmODBC.Width) / 2, (Screen.Height - frmODBC.Height) / 2
  317. End Sub
  318.  
  319.